blob: 576e9e6702e3b650c77a628e54361202cbcfd624 [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;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000110
Nico Huber3812a722016-10-12 12:12:51 +0200111 printf("\nidx val def\n");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000112
Nico Huber3812a722016-10-12 12:12:51 +0200113 for (k = 0; idx[k] != EOT; k++) {
114 printf("0x%02x: 0x%02x", idx[k], regval(port, idx[k]));
115
116 if (skip_def || def[k] == EOT) {
117 skip_def = 1;
118 printf("\n");
119 continue;
120 }
121 if (def[k] == NANA)
122 printf(" (NA)\n");
123 else if (def[k] == RSVD)
124 printf(" (RR)\n");
125 else if (def[k] == MISC)
126 printf(" (MM)\n");
127 else
128 printf(" (0x%02x)\n", def[k]);
129 }
130 } else {
131 printf("\nidx");
132 for (k = 0; idx[k] != EOT; k++) {
133 if (k && !(k % 8))
134 putchar(' ');
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000135 printf(" %02x", idx[k]);
Nico Huber3812a722016-10-12 12:12:51 +0200136 }
137
138 printf("\nval");
139 for (k = 0; idx[k] != EOT; k++) {
140 if (k && !(k % 8))
141 putchar(' ');
142 printf(" %02x", regval(port, idx[k]));
143 }
144
145 printf("\ndef");
146 for (k = 0; def[k] != EOT; k++) {
147 if (k && !(k % 8))
148 putchar(' ');
149 if (def[k] == NANA)
150 printf(" NA");
151 else if (def[k] == RSVD)
152 printf(" RR");
153 else if (def[k] == MISC)
154 printf(" MM");
155 else
156 printf(" %02x", def[k]);
157 }
Uwe Hermann2c290e32007-09-20 00:00:49 +0000158 }
159 printf("\n");
160}
161
162void dump_superio(const char *vendor,
163 const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000164 uint16_t port, uint16_t id, uint8_t ldn_sel)
Uwe Hermann519419b2007-09-16 20:59:01 +0000165{
Uwe Hermann2c290e32007-09-20 00:00:49 +0000166 int i, j, no_dump_available = 1;
Uwe Hermann519419b2007-09-16 20:59:01 +0000167
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000168 if (!dump)
169 return;
170
Uwe Hermann519419b2007-09-16 20:59:01 +0000171 for (i = 0; /* Nothing */; i++) {
172 if (reg_table[i].superio_id == EOT)
173 break;
174
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000175 if ((uint16_t)reg_table[i].superio_id != id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000176 continue;
177
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000178 for (j = 0; /* Nothing */; j++) {
Uwe Hermann519419b2007-09-16 20:59:01 +0000179 if (reg_table[i].ldn[j].ldn == EOT)
180 break;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000181 no_dump_available = 0;
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000182 dump_regs(reg_table, i, j, port, ldn_sel);
Uwe Hermann519419b2007-09-16 20:59:01 +0000183 }
Uwe Hermann25a6c0f2007-09-19 00:48:42 +0000184
Uwe Hermann2c290e32007-09-20 00:00:49 +0000185 if (no_dump_available)
186 printf("No dump available for this Super I/O\n");
Uwe Hermann519419b2007-09-16 20:59:01 +0000187 }
188}
189
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000190void dump_io(uint16_t iobase, uint16_t length)
191{
192 uint16_t i;
193
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000194 printf("Dumping %d I/O mapped registers at base 0x%04x:\n",
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000195 length, iobase);
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000196 for (i = 0; i < length; i++)
197 printf("%02x ", i);
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000198 printf("\n");
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000199 for (i = 0; i < length; i++)
200 printf("%02x ", INB(iobase + i));
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000201 printf("\n");
202}
203
Guenter Roecka89da092012-06-29 12:23:50 -0700204void dump_data(uint16_t iobase, int bank)
205{
206 uint16_t i;
207
208 printf("Bank %d:\n", bank);
209 printf(" ");
210 for (i = 0; i < 16; i++)
211 printf("%02x ", i);
212 set_bank(iobase, bank);
213 for (i = 0; i < 256; i++) {
214 if (i % 16 == 0)
215 printf("\n%02x: ", i / 16);
216 printf("%02x ", datareg(iobase, i));
217 }
218 printf("\n");
219}
220
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000221void probing_for(const char *vendor, const char *info, uint16_t port)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000222{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000223 if (!verbose)
224 return;
225
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000226 /* Yes, there's no space between '%s' and 'at'! */
Uwe Hermanneec5ff42008-03-01 18:49:39 +0000227 printf("Probing for %s Super I/O %sat 0x%x...\n", vendor, info, port);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000228}
229
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000230/** Print a list of all supported chips from the given vendor. */
231void print_vendor_chips(const char *vendor,
232 const struct superio_registers reg_table[])
233{
234 int i;
235
236 for (i = 0; reg_table[i].superio_id != EOT; i++) {
237 printf("%s %s", vendor, reg_table[i].name);
238
239 /* Unless the ldn is empty, assume this chip has a dump. */
240 if (reg_table[i].ldn[0].ldn != EOT)
241 printf(" (dump available)");
242
243 printf("\n");
244 }
245
246 /* If we printed any chips for this vendor, put in a blank line. */
247 if (i != 0)
248 printf("\n");
249}
250
251/** Print a list of all chips supported by superiotool. */
252void print_list_of_supported_chips(void)
253{
254 int i;
255
256 printf("Supported Super I/O chips:\n\n");
257
258 for (i = 0; i < ARRAY_SIZE(vendor_print_functions); i++)
259 vendor_print_functions[i].print_list();
260
Paul Menzela8843de2017-06-05 12:33:23 +0200261 printf("See <https://coreboot.org/Superiotool#Supported_devices> "
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000262 "for more information.\n");
263}
264
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000265static void print_version(void)
266{
Ulf Jordan39a5bf72007-10-13 18:06:12 +0000267 printf("superiotool r%s\n", SUPERIOTOOL_VERSION);
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000268}
269
Uwe Hermann2046ff92007-09-01 21:02:44 +0000270int main(int argc, char *argv[])
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000271{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000272 int i, j, opt, option_index;
Andriy Gaponb64aa602008-10-28 22:13:38 +0000273#if defined(__FreeBSD__)
274 int io_fd;
275#endif
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000276
Uwe Hermann246be7d2007-10-31 22:22:11 +0000277 static const struct option long_options[] = {
Uwe Hermanneddc4732007-09-20 23:57:44 +0000278 {"dump", no_argument, NULL, 'd'},
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000279 {"extra-dump", no_argument, NULL, 'e'},
Nico Huber3812a722016-10-12 12:12:51 +0200280 {"alternate-dump", no_argument, NULL, 'a'},
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000281 {"list-supported", no_argument, NULL, 'l'},
Uwe Hermanneddc4732007-09-20 23:57:44 +0000282 {"verbose", no_argument, NULL, 'V'},
283 {"version", no_argument, NULL, 'v'},
284 {"help", no_argument, NULL, 'h'},
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000285 {0, 0, 0, 0}
286 };
287
Nico Huber3812a722016-10-12 12:12:51 +0200288 while ((opt = getopt_long(argc, argv, "dealVvh",
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000289 long_options, &option_index)) != EOF) {
290 switch (opt) {
291 case 'd':
292 dump = 1;
293 break;
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000294 case 'e':
295 extra_dump = 1;
296 break;
Nico Huber3812a722016-10-12 12:12:51 +0200297 case 'a':
298 alternate_dump = 1;
299 break;
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000300 case 'l':
301 print_list_of_supported_chips();
302 exit(0);
303 break;
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000304 case 'V':
305 verbose = 1;
306 break;
307 case 'v':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000308 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000309 exit(0);
310 break;
311 case 'h':
Uwe Hermanne4749562007-09-19 16:26:18 +0000312 printf(USAGE);
Uwe Hermann969a9f62008-03-17 13:43:48 +0000313 printf(USAGE_INFO);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000314 exit(0);
315 break;
316 default:
317 /* Unknown option. */
318 exit(1);
319 break;
320 }
321 }
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000322
Andriy Gaponb64aa602008-10-28 22:13:38 +0000323#if defined(__FreeBSD__)
324 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
325 perror("/dev/io");
Fabian Groffen318ddb82023-03-10 18:12:25 +0100326#elif defined(__NetBSD__)
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000327 if (iopl(3) < 0) {
328 perror("iopl");
Fabian Groffen318ddb82023-03-10 18:12:25 +0100329#else
330 if (ioperm(0, 6000, 1) < 0) {
331 perror("ioperm");
Andriy Gaponb64aa602008-10-28 22:13:38 +0000332#endif
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000333 printf("Superiotool must be run as root.\n");
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000334 exit(1);
335 }
336
Uwe Hermannd937b522007-10-17 23:42:02 +0000337 print_version();
338
Carl-Daniel Hailfingerbb38f322010-01-24 01:40:46 +0000339#ifdef PCI_SUPPORT
340 /* Do some basic libpci init. */
341 pacc = pci_alloc();
342 pci_init(pacc);
343 pci_scan_bus(pacc);
344#endif
345
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000346 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
347 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
348 superio_ports_table[i].probe_idregs(
349 superio_ports_table[i].ports[j]);
350 }
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000351
Uwe Hermanne9d46162007-10-07 20:01:23 +0000352 if (!chip_found)
353 printf("No Super I/O found\n");
354
Andriy Gaponb64aa602008-10-28 22:13:38 +0000355#if defined(__FreeBSD__)
356 close(io_fd);
357#endif
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000358 return 0;
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000359}